<?php
require_once dirname(__FILE__) . '/../../../incls/php/dbutils.class.php';
require_once dirname(__FILE__) . '/../../../incls/php/miscutils.class.php';
require_once dirname(__FILE__) . '/../../../incls/php/config.php';
$myPdo = DbUtils::createPdoInst();
$page_id = MiscUtils::getParam('i', NULL);
$item_id = MiscUtils::getParam('id', 0);

$item = NULL;
$cond_vals = new stdClass();
$cond_vals->c = 't.id = :v1';
$cond_vals->v = array(':v1' => $item_id);
$tmp = DbUtils::get($myPdo,  TABLE_NAME_PREFIX_PLUGIN . 'ecom_brand', $cond_vals, NULL, NULL, NULL, NULL, NULL)->d;
if (count($tmp) > 0) {
  $item = $tmp[0];
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<style>
* {
  font-family: Trebuchet MS;
  font-size: 12px;
  padding: 0;
  margin: 0;
}
table.form {
  background-color: #fff;
  border-top: 1px solid #222;
  border-left: 1px solid #222;
}
table.form th.header {
  text-align: left;
  color: #fff;
  background-color: #222;
  padding-left: 5px;
  font-weight: bold;
  height: 25px;
  border-bottom: 1px solid #222;
  border-right: 1px solid #222;
}

table.form td {
  padding: 3px 5px; 
  vertical-align: middle;
}
table.form td.title {
  background-color: #666;
  border-bottom: 1px solid #222;
  border-right: 1px solid #222;
  width: 150px;
  color: #fff;
  font-weight: bold;
}
table.form td.content {
  width: 300px;
  padding: 0;
  border-bottom: 1px solid #222;
  border-right: 1px solid #222;
}
table.form td.content input {
  width: 98%;
  height: 20px;
  border: 0;
  padding-left: 2px;
}
.imgbtn {
  background-image: url(assets/imgs/save.png);
  background-repeat: no-repeat;
  background-position: 5px 2px;
  padding: 5px 5px 5px 30px;
  border: 1px solid #666;
  background-color: #fff;
  cursor: pointer;
  border-radius: 3px;
}
.imgbtn:hover {
  background-color: #eee;
}
ul.hztl li {
  display: inline;
  vertical-align: top;
}
table.form4 {
  background-color: #e9e9e9;
  border: 1px solid #666;
  color: #000;
  padding: 2px;
  padding-bottom: 20px;
  cursor: pointer;
  display: inline-block;
  margin: 2px;
  font-weight: bold;
}
</style>
</head>
<body>
<div>
  <table class="form" cellpadding=0 cellspacing=0>
    <tr><th class="header" colspan=2>Edit Brand</th></tr>
    <tr>
      <td class="title">Brand Name</td>
      <td class="content"><input value="<?php echo (($item) ? $item->name : ''); ?>" id="name" type="text"></input></td>
    </tr>
    <tr>
      <td class="title" style="vertical-align: top;">Icon <?php echo '(' . TN_WIDTH . 'x' . TN_HEIGHT . ')'; ?>
        <br>
        <div id="spanButtonPlaceHolder"></div>
        <div id="cancelBtnId" style="display: none;"></div>
      </td>
      <td  id="imgbox" class="content"" style="height: 90px; padding: 5px; vertical-align: top;"></td>
    </tr>
  </table>
  <button class="imgbtn" onclick="save();" style="margin-top: 5px;">Save</button>
  <div style="height: 200px"></div>
</div>
<script type="text/javascript" src="../../../libs/jquery/jquery-1.6.2.min.js"></script>
<script type="text/javascript" src="../../../incls/js/myutils.js"></script>
<script type="text/javascript" src="../../../libs/swfupload/swfupload.js"></script>
<script type="text/javascript" src="../../../libs/swfupload/swfupload.queue.js"></script>
<script type="text/javascript" src="../../../libs/swfupload/myupload2.js"></script>
<link rel="stylesheet" href="assets/css/prettyPhoto.css" type="text/css" media="screen" charset="utf-8" />
<script src="assets/js/jquery.prettyPhoto.js" type="text/javascript" charset="utf-8"></script>
<script type="text/javascript">
  var _file = '<?php echo $item ? $item->file : ''; ?>';

  $(function(){
    //NOT GOOD
    var div = parent.document.getElementById('<?php echo LAST_NAV; ?>');
    var div_parent = div.parentNode;
    var value = div_parent.firstChild.nodeValue;
    MyUtils.removeChildElements(div_parent);
    var elm;
    elm = parent.document.createElement('a');
    elm.href = '#';
    elm.style.color = '#000';
    elm.style.fontSize = '13px';
    elm.style.fontWeight = 'bold';
    elm.onclick = function() {
      var url = parent.document.URL.replace('&script=index_brand', '');
      url = url.replace('&id=<?php echo $item_id; ?>', '');
      url = url.replace('#', '');
      parent.location.href = url;
    };
    elm.appendChild(parent.document.createTextNode(value));
    div_parent.appendChild(elm);

    var td;
    td = div_parent.parentNode.insertCell(-1);
    td.style.width = '20px';
    td.style.textAlign = 'center';
    td.style.color = '#aaa';
    td.appendChild(parent.document.createTextNode('>'));
    
    td = div_parent.parentNode.insertCell(-1);
    td.style.height = '25px';
    td.style.color = '#333';
    td.style.fontSize = '13px';
    <?php if ($item) { ?> 
    td.title = '<?php echo $item->name; ?>';
    td.appendChild(parent.document.createTextNode(MyUtils.getShort('<?php echo $item->name; ?>', 10)));
    <?php } else { ?>
    td.appendChild(parent.document.createTextNode('New Brand'));
    <?php } ?>
    //NOT GOOD END
    
    new MyUpload('spanButtonPlaceHolder', 'upload.php', function(msgs) {
      if (msgs.length > 0) {
        _file = (msgs[0]);
        set_image(_file);
      }
    }, {
      dir: '../../../',
      button_image_url: 'assets/imgs/browse.png',
      button_width: 80,
      button_height: 25,
      file_upload_limit: 1,
      file_queue_limit: 1,
      file_types: '*.jpg;*.jpeg;*.png;*.gif',
      file_types_description: 'Image Files',
      cancelBtnId: 'cancelBtnId',
      post_params: {
        item_id: '<?php echo $item_id ?>'
      },
      debug: 0
    });
    set_image('<?php echo ($item) ? $item->file : ''; ?>');
  });
  
  function set_image(path) {
    var ul, div, elm;

    div = document.getElementById('imgbox');
    MyUtils.removeChildElements(div);

    if (path == '') {
      return;
    }
    
    div.appendChild(__get_img({
      id: <?php echo $item_id; ?>,
      file: _file
    }));
    __set_pp();
  };

  function save() {
    var obj = {
      <?php if ($item) { ?>
      id: '<?php echo $item->id; ?>',
      <?php } ?>
      file: _file,
      name: document.getElementById('name').value,
      is_tmp: 0
    }
    $.ajax({
      type: "POST",
      url: "<?php echo basename(__FILE__, '.php'); ?>.server.php",
      data: "action=save&q=" + encodeURIComponent(JSON.stringify(obj)),
      success: function(msg){
        var obj = JSON.parse(msg);
        parent.location.href = 'http://<?php echo URL . "admin.php?i=$page_id&script=index_brand&id="; ?>' + obj.id;
      }
    });
  }

  function __get_img(img_obj) {
    var table, tr, td, elm, a, img;
    table = document.createElement('table');
    table.className = 'form4';
    table.cellPadding = 0;
    table.cellSpacing = 0;
    tr = table.insertRow(-1);
    td = tr.insertCell(-1);
    td.style.textAlign = 'right';
    td.className = 'img';
    elm = document.createElement('img');
    elm.src = 'assets/imgs/del.png';
    elm.onclick = function() {
      var r = window.confirm('Are You Sure?');
      if (r) {
        $.ajax({
          type: "POST",
          url: "<?php echo basename(__FILE__, '.php'); ?>.server.php",
          data: "action=del_image&q=" + encodeURIComponent(img_obj.id),
          success: function(msg){
            _file = '';
            set_image('');
          }
        });
      }
    };
    td.appendChild(elm);
    tr = table.insertRow(-1);
    td = tr.insertCell(-1);
    a = document.createElement('a');
    a.rel = 'prettyPhoto[gallery1]';
    a.href = img_obj.file;
    td.appendChild(a);
    img = document.createElement('img');
    img.src = img_obj.file;
    img.style.width = '60px';
    a.appendChild(img);
    return table;
  }

  function __set_pp() {
    $("a[rel^='prettyPhoto']").prettyPhoto({
      social_tools: ''
    });
  }
</script>
</body>
</html>